package com.personal.test.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.personal.test.common.utils.ParamsUtils;
import com.personal.test.common.domin.R;
import com.personal.test.domin.User;
import com.personal.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


/**
 * @author linyh
 * @version 1.0
 * @email 1503386669@qq.com
 * @date 2020/10/22 9:26
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 数据保存
     * @param user
     * @return
     */
    @PostMapping("/save")
    public R save(User user){
        user.preSet();
        if(userService.save(user)){
            return R.success().setData("保存成功");
        }else{
            return R.error().setData("保存失败");
        }
    }

    @PostMapping("/update")
    public R update(User user){
        user.preUpdate();
        if(userService.updateById(user)){
            return R.success().setData("更新成功");
        }else{
            return R.error().setData("更新失败");
        }
    }

    /**
     * 查询列表(分页)
     * @param params
     * @return
     */
    @PostMapping("/list")
    public R list(@RequestBody Map<String, Object> params) {
        // 设置分页默认（一页10条数据）
        ParamsUtils.setParams(params);
        Page<User> query = new Page<>((int)params.get("pageNo"), (int)params.get("pageSize"), true);
        // 查询参数
        QueryWrapper queryWrapper = new QueryWrapper<User>();
        // 按创建时间降序查询
        queryWrapper.orderByDesc("create_date");
        // 分页查询
        IPage<User> page = userService.page(query,queryWrapper);
        // 数据封装
        Map<String,Object> dbData = new HashMap<>();
        dbData.put("rows",page.getRecords());
        dbData.put("pageSize",params.get("pageSize"));
        dbData.put("pageNo",params.get("pageNo"));
        dbData.put("total",page.getTotal());
        dbData.put("current",page.getCurrent());
        return R.success().setData(dbData);
    }

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @PostMapping("/form")
    public R form(String id){
        User user = userService.getById(id);
        return R.success().setData(user);
    }

    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @PostMapping("/logicDelete")
    public R logicDelete(String id){
        if(userService.removeById(id)){
            return R.success().setData("删除成功");
        }else{
            return R.error().setData("删除失败");
        }
    }

    /**
     * 物理删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public R delete(String id){
        if(userService.deleteById(id)){
            return R.success().setData("删除成功");
        }else{
            return R.error().setData("删除失败");
        }
    }
}
